package com.eightbears.beboy.base.util;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;

import com.eightbears.beboy.R;
import com.eightbears.beboy.SplashActivity;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.view.dialog.DefaultDialog;
import com.eightbears.beboy.base.view.dialog.MessageDialog;
import com.eightbears.beboy.entity.status.Status_Error;
import com.eightbears.beboy.ui.chat.DemoHelper;
import com.eightbears.beboy.ui.login.ForgetActivity;
import com.eightbears.beboy.ui.login.LoginActivity;
import com.eightbears.beboy.ui.login.PasswordLoginActivity;
import com.eightbears.beboy.ui.user.activity.HelpActivity;
import com.eightbears.beboy.ui.user.activity.InviteFriendsActivity;
import com.eightbears.beboy.ui.user.activity.PaymentActivity;
import com.eightbears.beboy.ui.user.activity.TransferActivity;

import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CEDE_LOGIN_FORGER_40302;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CEDE_LOGIN_NICKNAME_10004;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CEDE_LOGIN_NICKNAME_10006;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CEDE_LOGIN_NICKNAME_40005;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CEDE_LOGIN_PASSWORD_40102;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CEDE_LOGIN_UNREGISTER_40104;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CEDE_REPORT_40101;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CODE_FUNCTION_NO_OPEN_50001;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CODE_LOGIN_COUNT_50000;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CODE_LOGIN_PAST_40103;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CODE_LOGIN_REGISTERCODE_40002;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CODE_LOGIN_USEREDIT_10007;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CODE_SERVER_ERROR_500;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CODE_UNAUTHORIZED_401;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CODE_USER_INVITE_40001;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CODE_USER_PAY_40004;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CODE_USER_PAY_40105;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CODE_USER_PAY_40106;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CODE_USER_PAY_40107;
import static com.eightbears.beboy.ui.user.activity.TransferActivity.PARAMS_PAY_TRANSFER;


/**
 * Created by Bao on 2017/4/10.
 */

public class ErrorUtils {
    /**
     * 大部分用这个方法
     *
     * @param activity
     * @param status_error
     */
    public static void alertError(final Activity activity, Status_Error status_error) {
        DefaultDialog.Builder builder = new DefaultDialog.Builder(activity);

        final MessageDialog.Builder messageBuilder = new MessageDialog.Builder(activity);

        if (status_error == null)
            return;
        switch (status_error.getStatus_code()) {
            case ERROR_CODE_UNAUTHORIZED_401://token过期
                initDialog_401(activity, builder);
                break;
            case ERROR_CODE_SERVER_ERROR_500:
                ShowToast.showShortToast(activity.getResources().getString(R.string.error_server_error));
                break;
            case ERROR_CODE_LOGIN_REGISTERCODE_40002:
                ShowToast.showShortToast(activity.getString(R.string.error_security_code));
                break;
            case ERROR_CODE_LOGIN_COUNT_50000:
                ShowToast.showShortToast(activity.getResources().getString(R.string.error_server_error));
                break;
            case ERROR_CODE_FUNCTION_NO_OPEN_50001:
                ShowToast.showShortToast(activity.getResources().getString(R.string.error_function_no_open));
                break;
            case ERROR_CODE_LOGIN_PAST_40103:
                ShowToast.showShortToast(activity.getResources().getString(R.string.error_login_past_dut));
                activity.startActivity(new Intent(activity, LoginActivity.class));
                activity.moveTaskToBack(false);
                break;
            case ERROR_CODE_LOGIN_USEREDIT_10007:
                ShowToast.showShortToast(activity.getString(R.string.error_user_extist));
                break;
            case ERROR_CEDE_LOGIN_PASSWORD_40102:
                if (activity instanceof PasswordLoginActivity) {
                    ((PasswordLoginActivity) activity).getEt_pass().setText("");
                }
                ShowToast.showShortToast(activity.getString(R.string.error_username_or_password));
                break;
//            case ERROR_CODE_USER_PAY_40107:
//                ShowToast.showShortToast(activity.getString(R.string.error_password_lock));
//                break;
            case ERROR_CEDE_LOGIN_UNREGISTER_40104:
                ShowToast.showShortToast(activity.getString(R.string.error_phone_num_not_found));
                activity.startActivity(new Intent(activity, LoginActivity.class));
                activity.finish();
                break;
            case ERROR_CEDE_LOGIN_NICKNAME_10004:
                ShowToast.showShortToast(activity.getString(R.string.error_nickname_exist));
                break;
            case ERROR_CEDE_LOGIN_NICKNAME_10006:
                ShowToast.showShortToast(activity.getString(R.string.error_phone_num_input));
                break;
            case ERROR_CEDE_LOGIN_FORGER_40302:
                ShowToast.showShortToast(activity.getString(R.string.error_phone_number_existg));
                activity.startActivity(new Intent(activity, LoginActivity.class));
                activity.finish();
                break;
            case ERROR_CEDE_REPORT_40101:
                ShowToast.showShortToast(activity.getString(R.string.error_jurisdiction));
                break;
            case ERROR_CODE_USER_INVITE_40001:
                if (activity instanceof InviteFriendsActivity) {
                    ShowToast.showShortToast(activity.getString(R.string.error_invite_mlm));
                } else {
                    ShowToast.showShortToast(activity.getString(R.string.error_invite));
                }
                break;
            case ERROR_CODE_USER_PAY_40004:
                if (activity instanceof TransferActivity) {
                    ((TransferActivity) activity).getPasswordPayPopup().setTextClear();
                } else if (activity instanceof PaymentActivity) {
                    ((PaymentActivity) activity).getPasswordPayPopup().setTextClear();
                }
                ShowToast.showShortToast(activity.getString(R.string.error_user_pay));
                ((PaymentActivity) activity).getPasswordPayPopup().dismiss();
                break;
            case ERROR_CODE_USER_PAY_40105:
                initDialog_40105(activity, messageBuilder);
                break;
            case ERROR_CODE_USER_PAY_40107://支付密码锁定
                initDialog_40107(activity, messageBuilder);
                break;
            case ERROR_CODE_USER_PAY_40106://未设置支付密码
                initDialog_40106(activity, messageBuilder);
                break;
            case ERROR_CEDE_LOGIN_NICKNAME_40005:
                initDialog_40005(activity, builder);
                break;
        }
    }

    private static void initDialog_40106(final Activity activity, MessageDialog.Builder messageBuilder) {
        //设置支付密码
        messageBuilder.setForgetButton(activity.getString(R.string.text_repass), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (activity instanceof PasswordLoginActivity) {
                    ((BaseActivity) activity).startLocalActivity(activity, ForgetActivity.class);
                } else {
                    clearText(activity);
                    ((BaseActivity) activity).startLocalActivity(activity, ForgetActivity.class, PARAMS_PAY_TRANSFER, PARAMS_PAY_TRANSFER);
                }
            }
        });
        messageBuilder.setContent("您还没设置支付密码，请设置支付密码");
        messageBuilder.create().show();
    }

    private static void initDialog_401(final Activity activity, DefaultDialog.Builder builder) {
        exitApp();
        builder.setTitle(R.string.alert_text);
        builder.setMessage(R.string.error_login_past_dut);
        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                ((BaseActivity) activity).startLocalActivityAndClear(activity, SplashActivity.class);
            }
        });
        builder.create().show();
    }

    private static void initDialog_40005(final Activity activity, DefaultDialog.Builder builder) {
        builder.setTitle(activity.getString(R.string.alert_text));
        builder.setMessage(activity.getString(R.string.text_error_40005));
        builder.setPositiveButton(activity.getString(R.string.app_help), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                activity.startActivity(new Intent(activity, HelpActivity.class));
                dialog.dismiss();
            }
        });
        builder.setNegativeButton(activity.getString(R.string.btn_cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    private static void initDialog_40107(final Activity activity, MessageDialog.Builder messageBuilder) {
        //支付密码错误
        messageBuilder.setForgetButton(activity.getString(R.string.text_repass), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (activity instanceof PasswordLoginActivity) {
                    ((BaseActivity) activity).startLocalActivity(activity, ForgetActivity.class);
                } else {
                    clearText(activity);
                    ((BaseActivity) activity).startLocalActivity(activity, ForgetActivity.class, PARAMS_PAY_TRANSFER, PARAMS_PAY_TRANSFER);
                }
            }
        });
        messageBuilder.setContent(activity.getString(R.string.error_user_pay_password_close));
        messageBuilder.create().show();
    }

    private static void initDialog_40105(final Activity activity, MessageDialog.Builder messageBuilder) {
        //支付密码错误
        messageBuilder.setForgetButton(activity.getString(R.string.text_forget_password), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ((BaseActivity) activity).startLocalActivity(activity, ForgetActivity.class, PARAMS_PAY_TRANSFER, PARAMS_PAY_TRANSFER);
                clearText(activity);
            }
        });
        messageBuilder.setResetButton(activity.getString(R.string.text_reset), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                clearText(activity);
            }
        });
        messageBuilder.setContent(activity.getString(R.string.error_user_pay_password));
        messageBuilder.create().show();
    }

    private static void clearText(Activity activity) {
        if (activity instanceof TransferActivity) {
            ((TransferActivity) activity).getPasswordPayPopup().setTextClear();
        } else if (activity instanceof PaymentActivity) {
            ((PaymentActivity) activity).getPasswordPayPopup().setTextClear();
        }
    }

    //退出登录
    private static void exitApp() {
        DemoHelper.getInstance().logout(true, null);
        SPUtil.loginOut();
    }

}
